(function (fruit, support, dom, $, global,document, undefined) {
    var engine = fruit.event.engine;
    var rsimpleFileds = /^(?:text|password|textarea|file)$/i;
    var cache = fruit.cache;

    function getValue(inElement) {
        var type = inElement.type,
            val = inElement.value,
            result;
        switch (type) {
            case "radio":
            case "checkbox":
                result = inElement.name ? inElement.ownerDocument.getElementsByName(inElement.name) : [inElement];
                break;
            case "select-one":
                val = inElement.selectedIndex;
                break;
        }
        if (result) {
            for (var i = 0, ri = 0, re = [], elem; elem = result[i++];) {
                re[ri++] = elem["checked"];
            }
            val = re.join("-");
        }
        return val;
    }

    function initData(inDataString) {
        var dataString = inDataString || "__changeData";
        var el;
        document.onbeforeactivate = function () {
            if (el = global.event.srcElement) {
                cache.data(el, dataString, getValue(el));
            }
        };
    }

    function hasDataChange(inEvent, inType, inDataString) {
        var el;
        var dataString = inDataString || "__changeData";
        if (el = inEvent.target) {
            var cacheData = cache.data(el, dataString);
            var currentData = getValue(el);
            if (cacheData === undefined) {
                cacheData = "";
            }
            if (currentData !== cacheData) {
                cache.data(el, dataString, currentData);
                inEvent.type = inType;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    fruit.define('fruit.event.EnterLeaveProcessor', fruit.event.DefaultSimulateProcessor, {
        methods:{
            isTarget:function (inIdentity, inEvent) {
                var related = inEvent.relatedTarget,
                    target = inEvent.target,
                    element = engine.getById(inIdentity);
                return  related && !dom.contains(element, related, true) && dom.contains(element, target, true);
            },
            normalize:function (inEvent) {
                var event = this.inherited(inEvent);
                event.type = (event.type === "mouseover") ? "mouseenter" : "mouseleave";
                return event;
            }
        }
    });
    fruit.define('fruit.event.DragFixedProcessor', fruit.event.DefaultSimulateProcessor, {
        singleton:true,
        methods:{
            init:function () {
                this.dragEl = null;
                this.dragActive = false;
            },
            initData:function (dragEvents) {
                this.dragEvents = dragEvents || {
                    start:'mousedown',
                    move:'mousemove',
                    end:'mouseup'
                };
                return this.dragEvents;
            },
            isTarget:function (inIdentity, inEvent) {
                if (this.dragEl) {
                    if (inIdentity == dom.identity(inEvent.target)) {
                        if (inEvent.type == "dragstart") {
                            this.dragActive = true;
                        }
                    }
                    return this.dragActive;
                }
            },
            process:function (simType, srcType, processor) {
                var self = this;
                var event;
                var dragEvents = self.initData();
                if (dragEvents.start === srcType) {
                    engine.addEventListener(document, srcType, function (inEvent) {
                        event = self.normalize(inEvent);
                        self.dragEl = event.target;
                        engine.trigger(simType, event, processor);
                    });
                } else if (dragEvents.move === srcType) {
                    engine.addEventListener(document, srcType, function (event) {
                        if (self.dragActive) {
                            engine.trigger(simType, self.normalize(event), processor);
                        }
                    });
                } else {
                    engine.addEventListener(document, srcType, function (event) {
                        if (self.dragActive) {
                            engine.trigger(simType, self.normalize(event), processor);
                            self.dragActive = false;
                        }
                    });
                }
            },
            normalize:function (inEvent) {
                var event = this.inherited(inEvent);
                var dragEvents = this.dragEvents;
                switch (event.type) {
                    case dragEvents.start:
                        event.type = "dragstart";
                        break;
                    case dragEvents.move:
                        event.type = "drag";
                        break;
                    case dragEvents.end:
                        event.type = "dragend";
                        break;
                }
                return event;
            }
        }
    });
    fruit.define('fruit.event.ChangeProcessor', fruit.event.DefaultSimulateProcessor, {
        methods:{
            isTarget:function (inIdentity, inEvent) {
                var result = false;
                if (document.addEventListener) {
                    result = this.inherited(inIdentity, inEvent);
                } else {
                    result = ( inIdentity == dom.identity(inEvent.target)) && hasDataChange(inEvent, "change", "__changeData");
                }
                return result;
            },
            process:function (simType, srcType, processor) {
                var self = this;
                var event, iFormType, type;
                if (document.addEventListener) {
                    this.inherited(simType, srcType, processor, true);
                } else {
                    initData("__changeData");
                    document.onfocusout = document.onclick = document.onkeyup = function () {
                        event = global.event;
                        (iFormType = event.srcElement.type) && (iFormType = iFormType.toLowerCase());
                        type = event.type;
                        if ("focusout" == type || !rsimpleFileds.test(iFormType)) {
                            engine.trigger(simType, self.normalize(event), processor);
                        }
                    };
                }
            }
        }
    });
    fruit.define('fruit.event.InputProcessor', fruit.event.DefaultSimulateProcessor, {
        methods:{
            isTarget:function (inIdentity, inEvent) {
                var result = false;
                if (support.w3cInput) {
                    result = this.inherited(inIdentity, inEvent);
                } else {
                    result = inIdentity == dom.identity(inEvent.target) && hasDataChange(inEvent, "input", "__inputData");
                }
                return result;
            },
            process:function (simType, srcType, processor) {
                var self = this;
                if (support.w3cInput) {
                    this.inherited(simType, srcType, processor, true);
                } else {
                    initData("__inputData");
                    document.onkeyup = document.onkeypress = function () {
                        engine.trigger(simType, self.normalize(global.event), processor);
                    };
                }
            }
        }
    });
    fruit.define('fruit.event.FocusBlurProcessor', fruit.event.DefaultSimulateProcessor, {
        methods:{
            process:function (simType, srcType, processor) {
                this.inherited(simType, srcType, processor, true);
            },
            normalize:function (inEvent) {
                var event = this.inherited(inEvent);
                if (!document.addEventListener) {
                    event.type = (inEvent.type === "focusin") ? "focus" : "blur";
                }
                return event;
            }
        }
    });
    fruit.define('fruit.event.MouseWheelProcessor', fruit.event.FocusBlurProcessor, {
        methods:{
            normalize:function (inEvent) {
                var event = this.inherited(inEvent);
                if (!support.onmousewheel) {
                    event.type = 'mousewheel';
                }
                return event;
            }
        }
    });
    fruit.define('fruit.event.DOMNodeChanged', fruit.event.DefaultSimulateProcessor, {
        methods:{
            process:function (simType, srcType, processor) {
                if (srcType === 'DOMNodeInserted') {
                    this.inherited(simType, 'DOMNodeInserted', processor);
                    this.inherited(simType, 'DOMNodeRemoved', processor);
                }
            },
            normalize:function (inEvent) {
                var event = this.inherited(inEvent);
                event.type = 'DOMNodeChanged';
                return event;
            }
        }
    });
//    engine.registerProcessor(['DRAGSTART', 'DRAG', 'DRAGEND'], ['mousedown', 'mousemove', 'mouseup']);
    /*engine.registerProcessor(['DRAGSTART', 'DRAG', 'DRAGEND'], ['touchstart', 'touchmove', 'touchend'], fruit.event.DragFixedProcessor, null, {
     start:'touchstart',
     move:'touchmove',
     end:'touchend'
     });*/
    engine.registerProcessor(['DRAGSTART', 'DRAG', 'DRAGEND'], ['mousedown', 'mousemove', 'mouseup'], fruit.event.DragFixedProcessor);
    engine.registerProcessor(['MOUSEENTER', 'MOUSELEAVE'], ['mouseover', 'mouseout'], fruit.event.EnterLeaveProcessor);
    engine.registerProcessor('CHANGE', 'change', fruit.event.ChangeProcessor);
    engine.registerProcessor('INPUT', 'input', fruit.event.InputProcessor);
    if (document.addEventListener) {
        engine.registerProcessor(['FOCUS', 'BLUR'], ['focus', 'blur'], fruit.event.FocusBlurProcessor);
    } else {
        engine.registerProcessor(['FOCUS', 'BLUR'], ['focusin', 'focusout'], fruit.event.FocusBlurProcessor);
    }
    if (support.onmousewheel) {
        engine.registerProcessor('MOUSEWHEEL', 'mousewheel', fruit.event.MouseWheelProcessor);
    } else {
        engine.registerProcessor('MOUSEWHEEL', 'DOMMouseScroll', fruit.event.MouseWheelProcessor);
    }
    engine.registerProcessor('DOMNODECHANGED', 'DOMNodeInserted', fruit.event.DOMNodeChanged);
})(fruit, fruit.support, fruit.dom, jQuery, window, document,undefined);